/*
 * TracerException.java
 *
 * $Id: TracerException.java,v 1.1 2008-03-18 12:58:45 mario Exp $
 */
package org.ceteca.explica.core.traceability;

/**
 * This class encapsulates and holds all the errors that might have taken
 * place during any of the application tracer operations.
 * <br/>
 * Responsibilities:
 * <ul>
 * 	<li> Encapsulate and pass on the error messages to the upper layers. </li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class TracerException extends Exception {
	private static final long serialVersionUID = 10000009;
	
	/**
	 * Internal error code to support error localization and classification.
	 */
	private int errCode;
	/**
	 * Error message string that is showed to the end user.
	 */
	private String errMessage;

	/**
	 * Default TracerException constructor.
	 * @param errCode int, error code.
	 */
	public TracerException(int errCode) {
		super();
		this.errCode = errCode;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}
	
	/**
	 * TracerException constructor for assigning the exception
	 * that raised the error.
	 * @param errCode int, error code.
	 * @param cause Throwable, exception that raised the error.
	 */
	public TracerException(int errCode, Throwable cause) {
		super(cause);
		this.errCode = errCode;
		
		this.errMessage = this.getErrorMessage(this.errCode);
	}

	/**
	 * Sets up the error label that will be used for constructing
	 * the formatted error message. This label will be the base of the
	 * message, constructed also with the parameters specified and 
	 * using the internationalization mechanism to translate the message
	 * to the user's language it it's supported. 
	 * @param errMessage String, error label.
	 */
	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}
	/**
	 * Gets the error label that will be used for constructing
	 * the formatted error message. This label will be the base of the
	 * message, constructed also with the parameters specified and 
	 * using the internationalization mechanism to translate the message
	 * to the user's language it it's supported. 
	 * @return String, error label.
	 */
	public String getErrMessage() {
		return errMessage;
	}
	
	/**
	 * Gets the formatted error message text as it's showed
	 * on screen to the user.
	 * @return int, error code.
	 */
	public String getMessage() {
		return this.toString();
	}

	
	public String toString() {
		StringBuffer msg = new StringBuffer();
		msg.append("¡¡¡¡  ERROR ");
		msg.append(this.errCode);
		msg.append("  !!!!    ");
		
		msg.append("\t");
		msg.append(this.errMessage);
		
		return msg.toString();
	}

	/**
	 * Gets the error code returned to the user.
	 * @return int, error code.
	 */
	public int getErrCode() {
		return errCode;
	}
	/**
	 * Sets up the error code returned to the user.
	 * @param errCode int, error code.
	 */
	public void setErrCode(int errCode) {
		this.errCode = errCode;
	}
	
	/**
	 * Get the localized error message corresponding to the error code.
	 * @param errCode int, error code.
	 * @return String, localized error message corresponding to the error code.
	 */
	private String getErrorMessage(int errCode) {
		StringBuffer msg = new StringBuffer();
		
		// TO_DO: Obtener el correspondiente mensaje para ese codigo de error
		return msg.toString();
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: TracerException.java,v 1.1 2008-03-18 12:58:45 mario Exp $";
	}
}

